package com.ruihang.generalibrary.utils;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class OtherUtils {
    public static String makeFragmentName(int containerId, long id) {
        return "android:switcher:" + containerId + ":" + id;
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void close(Closeable... closeables) {
        if (closeables != null && closeables.length > 0) {
            for (int i = 0; i < closeables.length; i++) {
                close(closeables[i]);
            }
        }
    }

    public static void close(List<Closeable> list) {
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                close(list.get(i));
            }
        }
    }

    public static long getFileSize(File f) {
        if (f == null || !f.exists() || !f.isFile()) {
            return 0;
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            return fis.available();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
        }
        return 0;
    }

    public static <T> T getReferenceObj(WeakReference<T> wf) {
        if (wf != null) {
            return wf.get();
        }
        return null;
    }
}
